﻿String.prototype.trim = function () {
	return this.replace(/^\s+|\s+$/g, '');
};

String.prototype.ltrim = function () {
	return this.replace(/^\s+/, '');
};

String.prototype.rtrim = function () {
	return this.replace(/\s+$/, '');
};

String.prototype.padLeft = function (ch, num) {
	var re = new RegExp(".{" + num + "}$");
	var pad = "";

	if (!ch) ch = " ";
	do {
		pad += ch;
	} while (pad.length < num);

	return re.exec(pad + this)[0];
};

String.prototype.padRight = function (ch, num) {
	var re = new RegExp("^.{" + num + "}");
	var pad = "";

	if (!ch) ch = " ";
	do {
		pad += ch;
	} while (pad.length < num);

	return re.exec(this + pad)[0];
};

String.prototype.left = function (num) {
	if (num <= 0)
		return "";
	else if (num > this.length)
		return this;
	else
		return this.substring(0,num);
};

String.prototype.right = function (num) {
	if (num <= 0)
		return "";
	else if (num > this.length)
		return this;
	else
		return this.substring(this.length, this.length-num); 
};

String.prototype.reverse = function () {
	return this.split('').reverse().join('');
};

String.prototype.clean = function () {
	return parseFloat(this.replace(/[^0-9|.|-]/g, ''));
};

String.prototype.isEmail = function () {
	var exclude = /[^@\-\.\w]|^[_@\.\-]|[\._\-]{2}|[@\.]{2}|(@)[^@]*\1/;
	var check = /@[\w\-]+\./;
	var checkend = /\.[a-zA-Z]{2,3}$/;
	var email = this;

	if (((email.search(exclude) != -1) || (email.search(check)) == -1) || (email.search(checkend) == -1))
		return false;
	else
		return true;
};

String.prototype.stripAccents = function() {
    var in_chrs =  'àáâãäçèéêëìíîïñòóôõöùúûüýÿÀÁÂÃÄÇÈÉÊËÌÍÎÏÑÒÓÔÕÖÙÚÛÜÝ',
        out_chrs = 'aaaaaceeeeiiiinooooouuuuyyAAAAACEEEEIIIINOOOOOUUUUY', 
        transl = {};
    eval('var chars_rgx = /['+in_chrs+']/g');
    for(var i = 0; i < in_chrs.length; i++){ transl[in_chrs.charAt(i)] = out_chrs.charAt(i); }
    return this.replace(chars_rgx, function(match){
        return transl[match]; });
};

String.prototype.removeInvalidChar = function () {
	var str = this.replace(/;/g, ",");
	str = str.replace(/\\/g, "/");
	return str.stripAccents();
};

String.prototype.existInArray = function (array) {
	var index= -1;
	var str = this;
	$.each(array, function (i, item) {
		if ( str == item )
			index = i;
	});
	return index;
};
